//
//  UITableView+RowPosition.m
//  mmbang
//
//  Created by 杨世昌 on 14/11/10.
//  Copyright (c) 2014年 iyaya. All rights reserved.
//

#import "UITableView+RowPosition.h"
#import <objc/runtime.h>  

static const void *RowPositionDelegate = &RowPositionDelegate;
static const void *UITableViewCellNormalBgSetter = &UITableViewCellNormalBgSetter;
static const void *UITableViewCellHighlightBgSetter = &UITableViewCellHighlightBgSetter;

@implementation UITableView (RowPosition)
@dynamic rowPositionDelegate;

- (id<UITableViewRowPositionDelegate>)rowPositionDelegate
{
     return objc_getAssociatedObject(self, RowPositionDelegate);
}

- (void)setRowPositionDelegate:(id<UITableViewRowPositionDelegate>)rowPositionDelegate
{
    objc_setAssociatedObject(self, RowPositionDelegate, rowPositionDelegate, OBJC_ASSOCIATION_ASSIGN);
}

- (UITableViewCellNormalBg)tableViewCellNormalBg
{
    return objc_getAssociatedObject(self, UITableViewCellNormalBgSetter);
}

- (void)setTableViewCellNormalBg:(UITableViewCellNormalBg)tableViewCellNormalBg
{
    objc_setAssociatedObject(self, UITableViewCellNormalBgSetter, tableViewCellNormalBg, OBJC_ASSOCIATION_COPY);
}

- (UITableViewCellHighlightBg)tableViewCellHighlightBg
{
    return objc_getAssociatedObject(self, UITableViewCellHighlightBgSetter);
}

- (void)setTableViewCellHighlightBg:(UITableViewCellHighlightBg)tableViewCellHighlightBg
{
    objc_setAssociatedObject(self, UITableViewCellHighlightBgSetter, tableViewCellHighlightBg, OBJC_ASSOCIATION_COPY);
}


- (UITableViewRowPosition)rowPositionForIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rows = [self numberOfRowsInSection:indexPath.section];
    if (rows == 1) {
        return UITableViewRowPositionFull;
    } else if (indexPath.row == 0) {
        return UITableViewRowPositionTop;
    } else if (indexPath.row == rows-1) {
        return UITableViewRowPositionBottom;
    } else {
        return UITableViewRowPositionCenter;
    }
}

-(void)setTableViewCellBg:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath
{
    UITableViewRowPosition position = [self rowPositionForIndexPath:indexPath];
    UIImage *normalImage = nil;
    if ([self.rowPositionDelegate respondsToSelector:@selector(tableView:normalBgImageForRowPosition:)]) {
        normalImage = [self.rowPositionDelegate tableView:self normalBgImageForRowPosition:position];
    }
    else if (self.tableViewCellNormalBg) {
        normalImage = self.tableViewCellNormalBg(self,position);
    }
    UIImage *highlightImage = nil;
    if ([self.rowPositionDelegate respondsToSelector:@selector(tableView:highlightBgImageForRowPosition:)]) {
        highlightImage = [self.rowPositionDelegate tableView:self highlightBgImageForRowPosition:position];
    }
    else if (self.tableViewCellHighlightBg) {
        highlightImage = self.tableViewCellHighlightBg(self,position);
    }
    
    cell.backgroundView = [[UIImageView alloc] initWithImage:normalImage];
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:highlightImage];
}

@end
